package com.faiz.algorithm;

public class Flatten {
    private TreeNode pre = null;

    public void flatten(TreeNode root) {
        dfs(root, 0);
    }

    public void dfs(TreeNode root, int direction) {
        if (root == null) {
            return;
        }
        if (pre != null) {
            switch (direction) {
                case 0:
                    pre.left = root;
                    break;
                case 1:
                    pre.right = root;
                    break;
                default:
            }
        }
        pre = root;
        dfs(root.left, 0);
        dfs(root.right, 1);
    }

    public static void main(String[] args) {
        Integer[] treeValue = {1, 2, 5, 3, 4, null, 6};
        TreeNode root = TreeNode.of(treeValue, 0);
        Flatten flatten = new Flatten();
        flatten.flatten(root);
    }
}
